package com.exam.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.exam.handler.StpHandler;
import com.exam.service.LessonService;
import com.exam.service.StudentService;
import com.exam.utils.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "班级管理")
@RestController
@RequestMapping(value = "/lesson")
public class LessonController {

    @Autowired
    private LessonService lessonService;

    @Autowired
    private StudentService studentService;

    @PostMapping("/join")
    @ApiOperation(value = "学生加入班级", notes = "学生加入班级")
    @SaCheckPermission("student")
    public ResultBody join(@RequestParam("uuid") String uuid) {
        return ResultBody.success(lessonService.join(StpHandler.getId(), uuid));
    }

    @PostMapping("/addLesson")
    @ApiOperation(value = "添加课程", notes = "添加课程")
    @SaCheckPermission("teacher")
    public ResultBody addLesson(@RequestParam("name") String name, @RequestParam("subjectId") int subjectId) {
        return ResultBody.success(lessonService.addLesson(name, subjectId, StpHandler.getId()));
    }

    @GetMapping("/getLessonByTeacherId")
    @ApiOperation(value = "查找该教师的所有课程", notes = "查找该教师的所有课程")
    @SaCheckPermission("teacher")
    public ResultBody getLessonByTeacherId() {
        return ResultBody.success(lessonService.getLessonListByTeacherId(StpHandler.getId()));
    }

    @GetMapping("/getLessonById/{lessonId}")
    @ApiOperation(value = "按id查找课程", notes = "按id查找课程")
    @SaCheckPermission("teacher")
    public ResultBody getLessonById(@PathVariable Integer lessonId) {
        return ResultBody.success(lessonService.getLessonById(lessonId));
    }

    @GetMapping("/getStudentByLessonId/{lessonId}")
    @ApiOperation(value = "按课程id查找学生", notes = "按课程id查找学生")
    @SaCheckPermission("teacher")
    public ResultBody getStudentByLessonId(@PathVariable Integer lessonId) {
        return ResultBody.success(lessonService.getStudentByLessonId(lessonId));
    }

    @PostMapping("/deleteStudentInLesson")
    @ApiOperation(value = "将学生踢出班级", notes = "将学生踢出班级")
    @SaCheckPermission("teacher")
    public ResultBody deleteStudentInLesson(@RequestParam("lessonId") Integer lessonId,@RequestParam("studentId") String studentId) {
        return ResultBody.success(lessonService.deleteStudentInLesson(lessonId, studentId));
    }

    @GetMapping("/getLessonListByStudentId")
    @ApiOperation(value = "获得学生的班级列表", notes = "获得学生的班级列表")
    @SaCheckPermission("student")
    public ResultBody getLessonListByStudentId() {
        return ResultBody.success(lessonService.getLessonListByStudentId(StpHandler.getId()));
    }

    @GetMapping("/getStudentInfoById")
    @ApiOperation(value = "获得学生信息", notes = "获得学生信息")
    @SaCheckPermission("teacher")
    public ResultBody getStudentInfoById(@RequestParam String studentId) {
        return ResultBody.success(studentService.getStudentInfo(studentId));
    }
}
